package com.hxx.common.core.wcontour.util;


import com.hxx.common.core.wcontour.base.TrainData;

/**
 * 抽稀网格点
 *
 * @author hxd
 */
public class GridRarefyUtil {

    /**
     * 网格数据抽稀点数
     * 进行网格IDW插值分析抽稀，不需要转换坐标
     * @param trainData 返回对象 data数组为[y][x]
     * @param rarefy    抽稀倍数
     */
    public static void rarefy(TrainData trainData, int rarefy) {
        if (rarefy <= 1) {
            return;
        }
        double[][] data = trainData.getData();
        double[] _X = trainData.get_X();
        double[] _Y = trainData.get_Y();

        int _XSize = trainData.getSize()[0];
        int _YSize = trainData.getSize()[1];
        // 余数
        boolean remainderX = _XSize % rarefy == 0;
        boolean remainderY = _YSize % rarefy == 0;

        // 抽稀最少倍数
        int aliquotX = _XSize / rarefy;
        int aliquotY = _YSize / rarefy;

        // 抽稀后的x和y的长度
        int rarefyX = remainderX ? aliquotX : aliquotX + 1;
        int rarefyY = remainderY ? aliquotY : aliquotY + 1;

        double[] lonArr = new double[rarefyX];
        double[] latArr = new double[rarefyY];
        for (int i = 0; i < _XSize; i++) {
            // 下标
            int index = i / rarefy;
            if (i % rarefy == 0) {
                lonArr[index] = _X[i];
            } else if (aliquotX == index && i == _XSize - 1) {
                // 最后一个不能被整除的点
                lonArr[rarefyX - 1] = _X[i];
            }
        }
        for (int i = 0; i < _YSize; i++) {
            int index = i / rarefy;
            if (i % rarefy == 0) {
                latArr[index] = _Y[i];
            } else if (aliquotY == index && i == _YSize - 1) {
                // 最后一个不能被整除的点
                latArr[rarefyY - 1] = _Y[i];
            }
        }
        trainData.setSize(new int[]{rarefyX, rarefyY});
        trainData.set_X(lonArr);
        trainData.set_Y(latArr);
        // 网格点及网格值
        double[][] matrix = new double[rarefyY][rarefyX];
        for (int i = 0; i < data.length; i++) {
            // 最后一个点不能丢弃
            if (i % rarefy != 0 && i != _YSize - 1) {
                continue;
            }
            for (int j = 0; j < data[0].length; j++) {
                // 最后一个点不能丢弃
                if (j % rarefy == 0) {
                    matrix[i / rarefy][j / rarefy] = data[i][j];
                } else if (j == _XSize - 1) {
                    matrix[rarefyY - 1][rarefyX - 1] = data[i][j];
                }
            }
        }
        trainData.setData(matrix);
    }
}
